package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.SocialSecurity;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.respBean.ListSocialResp;
import com.chtw.oasystem.service.impl.SocialSecurityServiceImpl;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author chtw
 * @date 2020-11-20 22:46
 */
@RequestMapping("/social")
@ResponseBody
@RestController
public class SocialSecurityController {

    private static final Logger logger = LoggerFactory.getLogger(SocialSecurityController.class);

    @Autowired
    private SocialSecurityServiceImpl socialSecurityService;

    @GetMapping("/list")
    public RespUtils<ListSocialResp> list(@RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false)int limit, String search) {
        try {
            PageInfo<SocialSecurity> info = socialSecurityService.list(search, page, limit);
            if (info != null) {
                int total = (int) info.getTotal();
                int page_total = info.getPages();
                return RespUtils.success(new ListSocialResp(info.getList(), total, page_total));
            }
            return RespUtils.fail("获取列表失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/create")
    public RespUtils<Boolean> create(HttpSession session, @RequestBody SocialSecurity socialSecurity) {
        try {
            boolean isCreate = socialSecurityService.create(session, socialSecurity);
            if (isCreate){
                return RespUtils.success(true);
            }
            return RespUtils.fail("添加社保信息失败");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/edit")
    public RespUtils<Boolean> edit(HttpSession session, @RequestBody SocialSecurity socialSecurity) {
        try {
            boolean isEdit = socialSecurityService.edit(session, socialSecurity);
            if (isEdit){
                return RespUtils.success(true);
            }
            return RespUtils.fail("修改社保信息失败");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    public RespUtils<Boolean> edit(HttpSession session, int id) {
        try {
            boolean isDel = socialSecurityService.delete(session, id);
            if (isDel){
                return RespUtils.success(true);
            }
            return RespUtils.fail("删除社保信息失败");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/info")
    public RespUtils<SocialSecurity> info(int id) {
        try {
            return RespUtils.success(socialSecurityService.info(id));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
